package com.app.freshmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegistActivity extends AppCompatActivity {
    EditText mEtUsername;
    EditText mEtRelname;
    EditText mEtEmail;
    EditText mEtTelphone;
    EditText mEtPassword;
    EditText mEtRelpassword;
    Button mBtnRegist;
    Spinner SpDepartments,SpMajors;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initview();
        initEvent();


    }

    private void initEvent() {
        mBtnRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regist();
            }
        });
    }

    private void regist() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JSONObject object = new JSONObject();
        try {
            object.put("name",mEtUsername.getText().toString());
            object.put("relname",mEtRelname.getText().toString());
            object.put("password",mEtPassword.getText().toString());
            object.put("departments",SpDepartments.getSelectedItem().toString());
            object.put("role","学生");
            object.put("major",SpMajors.getSelectedItem().toString());

            object.put("email",mEtEmail.getText().toString());
            object.put("telephone",mEtTelphone.getText().toString());


        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request =new JsonObjectRequest(Request.Method.POST,
                "http://106.12.30.77:8083/freshmenapp/user/login",
                object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Boolean success = jsonObject.optBoolean("success", false);
                        if (success == true) {
                            Toast.makeText(RegistActivity.this, "成功",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RegistActivity.this,"失败",
                                    Toast.LENGTH_SHORT).show();

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(RegistActivity.this,"网络访问错误",
                        Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    private void initview() {
        mBtnRegist = findViewById(R.id.btn_regist);
        mEtUsername = findViewById(R.id.et_username);
        mEtRelname = findViewById(R.id.et_relname);
        mEtEmail = findViewById(R.id.et_email);
        mEtTelphone = findViewById(R.id.et_telphone);
        mEtPassword = findViewById(R.id.et_password);
        mEtRelpassword = findViewById(R.id.et_relpassword);
        SpDepartments = findViewById(R.id.sp_departments);
        SpMajors = findViewById(R.id.sp_majors);


    }
}
